package com.module.prms.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;

import com.module.prms.ReqBaseBean;
import com.module.prms.dao.MenuDao;
import com.module.prms.vo.FMenuTree;
import com.module.prms.vo.PrmsMenu;

/**
 * 
 * <权限-菜单服务类>
 * 
 * @author huhui
 * @version [1.0, July 15, 2011]
 * @see [相关类/方法]
 * @since [二代产品/权限组件]
 */
public class MenuManager extends GenericHibernateManager<PrmsMenu, String> {

	static Logger logger = Logger.getLogger(MenuManager.class.getName ()) ;
	private MenuManager() {
	}

	private static MenuManager manager;

	public synchronized static MenuManager getInstance() {
		if (manager == null)
			manager = new MenuManager();
		return manager;
	}

	MenuDao mdao = MenuDao.getInstance();

	/**
	 * <保存> <将菜单信息保存到数据库>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	
	public boolean add(PrmsMenu obj) throws Exception{
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		boolean isTrue = false;
		try {
			isTrue = mdao.saveOrUpdate(session, obj);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return isTrue;

	}

	/**
	 * <删除> <将菜单信息从数据库删除>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void delmenu(PrmsMenu obj) throws Exception{
		this.delete(obj);
	}

	/**
	 * <修改> <修改数据库中的菜单信息>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void updmenu(PrmsMenu obj) throws Exception{
		this.update(obj);
	}

	/**
	 * 查询所有，不需分页
	 * 
	 * @param bean
	 */
	public FMenuTree list() throws Exception{
		Session session = this.getSession();
		FMenuTree fmt = null;
		try {
			fmt = mdao.getmenutree(session);
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return fmt;
	}

	/**
	 * 根据id查找数据
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public PrmsMenu findebyid(PrmsMenu bean) throws Exception{
		PrmsMenu objmenu = this.get(bean.getCmenuId());
		return objmenu;
	}

	/**
	 * 删除根据用户id
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	
	public boolean remove(List<String> list) throws Exception{
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		boolean isTrue = false;
		try {
			for (String s : list) {
				isTrue = mdao.deleteByKey(session,s);
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return isTrue;
	}
    
	@Override
	public GenericHibernateDao<PrmsMenu, String> getDao() {
		return mdao;
	}

}
